package EBM.BLL;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

class ServeOneClient extends Thread {
	private Socket socket;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Object cmd;
	private List<Object> request;
	private List<Object> response;
	private Object tmp;
	private Operation operation;

	public ServeOneClient(Socket s)
	throws IOException, InterruptedException {
		socket = s;
		in = new ObjectInputStream(socket.getInputStream()); 
		out = new ObjectOutputStream(socket.getOutputStream()); 
		request = new ArrayList<Object>();
		operation = new Operation();

		sleep(100);
		start();
	}

	public void run(){
		try{		
			//命令，所要做的操作
			cmd = in.readObject();
			System.out.println(cmd);
			//得到参数
			do{
				tmp = in.readObject();
				if(tmp.toString().equals("_E_N_D_"))
					break;
				System.out.println(tmp);
				request.add(tmp);
			}while(true);
			System.out.println("request--------end");

			try {
				try {
					response = operation.GetResponse(cmd, request);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}


			//返回参数
			for(int i=0;i<response.size();i++){
				System.out.println(response.get(i));
				out.writeObject(response.get(i));
			}
			System.out.println("response-------end");
			response.clear();        //清空返回参数列表
			out.writeObject("_E_N_D_");
			out.flush();

			System.out.println("cloing...");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			try{
				socket.close();
			}catch(IOException ex){
				System.out.println("Socket not closed");
			}
		}
	}
}

public class SocketConnection
{ 
	//声明ServerSocket类对象 
	private static ServerSocket serverSocket; 

	//声明并初始化服务器端监听端口号常量 
	public static final int PORT = 4321; 

	//声明InetAddress类对象ip，用于获取服务器地址及端口号等信息 
	private InetAddress ip;

	public SocketConnection(){
		try{
			ip = InetAddress.getLocalHost(); 
		}catch(IOException ex){
			System.out.println("Geting IP failed");
		}
	}

	public static void StartServe()
	throws IOException, InterruptedException {

		//以PORT为服务端端口，创建serverSocket对象以监听该端口上的连接 
		serverSocket = new ServerSocket(PORT);

		System.out.println("Server Started");

		try{
			while(true){
				//创建Socket类的对象socket，用于保存连接到服务器的客户端socket对象 
				Socket socket = serverSocket.accept();
				try{
					new ServeOneClient(socket);
				}catch(IOException ex){
					socket.close();
				}
			}
		}finally{
			serverSocket.close();
		}
	}
	
	public static void StopServe(){
		try {
			serverSocket.close();
			System.out.println("Server Stoped");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public InetAddress getIp() {
		return ip;
	}

	public void setIp(InetAddress ip) {
		this.ip = ip;
	}

//	public static void main(String[] args) throws IOException{
//		SocketConnection.StartServe();
//	}
}
